import QtQuick 2.9
import QtQuick.Window 2.2

Window {
    id: win
    visible: true
    width: _WIDTH
    height: _HEIGHT
    title: qsTr("跑男")

    property var treesArray: new Array
    property real _TREESPACETIME: 1500
    property real _WIDTH: 1920
    property real _HEIGHT: 720
    property real _MOVESPEED: 10
    property real treeTimes: 0
    property real fps: 30
    property bool gameover: false
    property real score: 0

    Timer{
        id: timer
        running: true
        interval: fps
        repeat: true
        onTriggered: {
            if(!gameover)
            {
                bg.update()
                player.update()
                win.update()
            }
        }
    }

    function reset()
    {
        for(var i in treesArray)
        {
            treesArray[i].destroy()
        }
        treesArray = new Array
        score = 0
        player.isJump = false
        player.y = player.groundposY
        gameover = false
    }

    // AABB
    // 优化
    function checkCollision(objA, objB)
    {
        var objACenter = Qt.point(objA.x+objA.width/2, objA.y+objA.height/2)
        var objBCenter = Qt.point(objB.globalX+objB.width/2, objB.globalY+objB.height/2)

        var xSpace = 0
        var ySpace = 0

        // 恐龙跳过树后，精确检测碰撞
        if(objBCenter.x > objACenter.x)
        {
            xSpace = 100
            ySpace = 100
        }


        if(Math.abs(objACenter.x - objBCenter.x) <= objA.width/2+objB.width/2 - xSpace
                && Math.abs(objACenter.y - objBCenter.y) <= objA.height/2+objB.height/2 - ySpace)
        {
            return true
        }

        return false
    }

    function checkCollisionScore(objA, objB)
    {
        if(!objB.enabledScore) return false

        var r = checkCollision(objA, objB)
        return r
    }

    function update()
    {
        // 创建树
        treeTimes -= fps
        if(treeTimes <= 0){
            treeTimes = _TREESPACETIME
            var obj = Qt.createComponent("Tree.qml").createObject(itemEnemys)
            treesArray.push(obj)
            Math.seed = (new Date).toLocaleString()
            var r = Math.random()
            obj.x = _WIDTH + 100 * r
            obj.y = 436
            obj.rect.enabledScore = true
        }

        // move tree
        for(var i in treesArray)
        {
            treesArray[i].x -= _MOVESPEED

            // check score
            var rScore = checkCollisionScore(player, treesArray[i].rect)
            if(rScore)
            {
                treesArray[i].rect.enabledScore = false
                score++
            }

            // check Collision
            var result = checkCollision(player, treesArray[i])
            if(result)
            {
                gameover = true
                break
            }

            //del tree
            if(treesArray[i].x < -treesArray[i].width * 2)
            {
                treesArray[i].destroy()
                treesArray.splice(i, 1)
            }
        }
    }


    Rectangle{
        id: root
        width: _WIDTH
        height: _HEIGHT
        color: "white"

        focus: true
        Keys.enabled: true
        Keys.onPressed: {
            if(event.key === Qt.Key_Space)
            {
                // reset
                if(gameover)
                    reset()
            }
        }

        BG{
            id: bg
            anchors.fill: parent
        }

        Item{
            id: itemEnemys
            anchors.fill: parent
        }

        Player{
            id: player
            y: groundposY
            x: _WIDTH / 3
        }

        Item{
            id: itemScore
            anchors.fill: parent
            Text{
                font.family: "微软雅黑"
                font.pixelSize: 30
                color: "black"
                text: score
                x: 5
                y: 5
            }
        }

        Image {
            id: imgGameover
            source: "qrc:/images/gameover.png"
            visible: gameover
            anchors.centerIn: parent
            z: 1

            Image {
                source: "qrc:/images/pause.png"
                anchors.horizontalCenter: parent.horizontalCenter
                anchors.top:  parent.bottom
                anchors.topMargin: 20
            }
        }

        MouseArea{
            anchors.fill: parent
            onClicked: {
                player.jump()
            }
        }
    }

}
